package com.example.zqm.git72.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zqm.git72.JavaBean.Mine;
import com.example.zqm.git72.Model.ActivityModel;
import com.example.zqm.git72.Model.TrainModel;
import com.example.zqm.git72.Model.UserModel;
import com.example.zqm.git72.R;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class TrainOrderActivity extends AppCompatActivity {
    private TextView jiesuan;
    private EditText name1,name2,cardid1,cardid2,tel;
    private String activityname,activityprice,activityprice1,activityintro,activity_id,payall;
    private TextView activity_name,activity_price,activity_price1,activity_intro;
    private TextView uname,priceall;
    private SharedPreferences sp;
    private String  user_id,paytype;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trainorder);
        initViews();
        //initData();
         getUserTel();
        baoming();

    }
    private void initViews() {

        Intent intent = getIntent();
        activity_id = intent.getStringExtra("id");
        activity_name=(TextView)findViewById(R.id.activityname1);
        activityname = intent.getStringExtra("trainname");
        activity_name.setText(activityname);

        activity_price=(TextView)findViewById(R.id.price);
        activityprice = intent.getStringExtra("trainprice");
        activity_price.setText(activityprice);


        activity_intro=(TextView)findViewById(R.id.need);
        activityintro = intent.getStringExtra("trainintro");
        activity_intro.setText(activityintro);

        name1=(EditText) findViewById(R.id.name1);
        name2=(EditText) findViewById(R.id.name2);
        cardid1=(EditText) findViewById(R.id.cardid1);
        cardid2=(EditText) findViewById(R.id.cardid2);
        tel=(EditText)findViewById(R.id.tel);
        jiesuan=(TextView)findViewById(R.id.jiesuan);
        uname=(TextView)findViewById(R.id.participantname);
          priceall=(TextView)findViewById(R.id.priceAll);
          payall=intent.getStringExtra("trainprice");
          priceall.setText(payall);
    }

        public void getUserTel(){
            SharedPreferences sharedPreferences=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
            user_id=sharedPreferences.getString("user_id","");
            UserModel userModel =new UserModel();
            Call<Mine> call =userModel.getUser(user_id);
            call.enqueue(new Callback<Mine>() {
            @Override
            public void onResponse(Call<Mine> call, Response<Mine> response) {
                  Mine userinfo=response.body();
                uname.setText(userinfo.getUser_name());
            }

            @Override
            public void onFailure(Call<Mine> call, Throwable t) {

            }
        });
    }
    public  void baoming(){
        jiesuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String participantname = uname.getText().toString();
                String participanttel = tel.getText().toString();
                String train_id=activity_id;
                String userid=user_id;
                String pay=payall;

                TrainModel trainModel =new TrainModel();
                Call<Integer> call =trainModel.signtrain(participantname,train_id,participanttel,pay,userid);
                call.enqueue(new Callback<Integer>() {
                    @Override
                    public void onResponse(Call<Integer> call, Response<Integer> response) {
                            Toast.makeText(TrainOrderActivity.this,"报名成功",Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onFailure(Call<Integer> call, Throwable t) {

                    }
                });

            }
        });

    }
}
